package leetcode.秋招.美团;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/8 10:33
 * @description：
 */
public class 序列问题 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n + 1];
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        map.put(0, Collections.singletonList(0));
        for (int i = 1; i < nums.length; i++) {
            nums[i] = in.nextInt();
            List<Integer> list = map.getOrDefault(nums[i], new ArrayList<>());
            list.add(i);
            map.put(nums[i], list);
        }
        System.out.println(Collections.unmodifiableMap(map));
        Arrays.sort(nums);
        int sum = 0;
        for (int i = 1; i < nums.length; i++) {
            List<Integer> list = map.get(nums[i]);
            int index = i;
            while (map.get(nums[--index]).get(0) > list.get(0))
            for (Integer integer : list) {
                System.out.println(integer + " " + nums[index]);
                sum += integer * nums[index];
            }
        }
        System.out.println(sum);
    }

    private static int binarySearch(int[] nums, int tar) {
        int l = 0;
        int r = nums.length - 1;
        Arrays.sort(nums);
        while(l < r) {
            int mid = l + r + 1 >> 1;
            if (nums[mid] < tar) {
                l = mid;
            } else
                r = mid - 1;
        }
        return nums[l] < tar ? nums[l] : 0;
    }
}
